package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyRongYinKeJi;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 融英科技-安心分期-ax
 * @ Author 江小游
 * @ Date 2025/6/3 14:37
 */
@ApiPlan(productId = "226", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5RongYinKeJi implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> newMap.put("md5", apiDataExchangeParameter.getPhoneMd5())));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 201)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyRongYinKeJi applyRongYinKeJi = new OrgApplyRongYinKeJi();
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        // 姓名
        applyRongYinKeJi.setName(apiApplyParameter.getUserName());
        // 手机号
        applyRongYinKeJi.setPhone(apiApplyParameter.getPhone());
        // 城市
        applyRongYinKeJi.setCity(apiApplyParameter.getCity().replace("市", ""));
        // 贷款金额
        applyRongYinKeJi.setMoney(String.valueOf(apiFieldConverter.getLoanAmountInt()));
        // 来源
        applyRongYinKeJi.setSource("安心分期");
        // 性别
        applyRongYinKeJi.setSex(apiFieldConverter.getSexText());
        // 年龄
        applyRongYinKeJi.setAge(apiApplyParameter.getAge());
        StringBuilder stringBuilder = new StringBuilder();
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无车产")
                    .when(apiApplyParameter.getCarInfo(), "2", "有车不抵押")
                    .when(apiApplyParameter.getCarInfo(), "3", "有车可抵押")
                    .when(apiApplyParameter.getCarInfo(), "4", "有车")
                    .end();
            stringBuilder.append("车辆情况：").append(text).append(";").append("\n");
        }
        //  房产情况
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "无房产")
                    .when(apiApplyParameter.getRealEstateInfo(), "2", "有房不抵押")
                    .when(apiApplyParameter.getRealEstateInfo(), "3", "有房可抵押")
                    .when(apiApplyParameter.getRealEstateInfo(), "4", "有房产")
                    .when(apiApplyParameter.getRealEstateInfo(), "5", "有按揭房产")
                    .when(apiApplyParameter.getRealEstateInfo(), "6", "有全款房产")
                    .end();
            stringBuilder.append("房产情况：").append(text).append(";").append("\n");
        }
        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "无公积金")
                    .when(apiApplyParameter.getProvidentFund(), "2", "缴纳6个月以下")
                    .when(apiApplyParameter.getProvidentFund(), "3", "缴纳6个月以上")
                    .when(apiApplyParameter.getProvidentFund(), "4", "有公积金")
                    .end("无");
            stringBuilder.append("公积金：").append(text).append(";").append("\n");
        }
        //   保险保单
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "无保险保单")
                    .when(apiApplyParameter.getInsurancePolicy(), "2", "缴纳未满1年")
                    .when(apiApplyParameter.getInsurancePolicy(), "3", "缴纳1年以上")
                    .when(apiApplyParameter.getInsurancePolicy(), "4", "有保险保单")
                    .end("无");
            stringBuilder.append("保险保单：").append(text).append(";").append("\n");
        }
        // 社保缴纳
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "无社保")
                    .when(apiApplyParameter.getSocialSecurity(), "2", "缴纳6个月以下")
                    .when(apiApplyParameter.getSocialSecurity(), "3", "缴纳6个月以上")
                    .when(apiApplyParameter.getSocialSecurity(), "4", "有社保")
                    .end("无");
            stringBuilder.append("社保缴纳：").append(text).append(";").append("\n");
        }
        // 营业执照注册年限
        if (ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", "未注册")
                    .when(apiApplyParameter.getBizLicenseYear(), "2", "注册1年以下")
                    .when(apiApplyParameter.getBizLicenseYear(), "3", "注册1~5年")
                    .when(apiApplyParameter.getBizLicenseYear(), "4", "注册5年以上")
                    .end();
            stringBuilder.append("营业执照：").append(text).append(";").append("\n");

        }
        applyRongYinKeJi.setRemark(stringBuilder.toString());
        return JsonUtils.toJson(applyRongYinKeJi);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
